﻿using System;
using System.Collections.Generic;
using BuzzX8.Xmpp.StreamElements.Stanza;

namespace BuzzX8.Xmpp.StreamElements.Builders
{
    public class PresenceStanzaBuilder : StanzaBuilder<PresenceStanzaType, PresenceStanza>
    {
        protected override PresenceStanzaType GetStanzaTypeForAttributeValue(string value)
        {
            switch (value)
            {
                case "":
                    return PresenceStanzaType.None;

                case "error":
                    return PresenceStanzaType.Error;

                case "probe":
                    return PresenceStanzaType.Probe;

                case "subscribe":
                    return PresenceStanzaType.Subscribe;

                case "subscribed":
                    return PresenceStanzaType.Subscribed;

                case "unavailable":
                    return PresenceStanzaType.Unavailable;

                case "unsubscribe":
                    return PresenceStanzaType.Unsubscribe;

                case "unsubscribed":
                    return PresenceStanzaType.Unsubscribed;

                default:
                    throw new NotImplementedException();
            }
        }

        protected override IEnumerable<string> GetSupportedElementNames()
        {
            return new[] {"presence"};
        }

        protected override PresenceStanza CreateNewElement(string name)
        {
            if (name == "presence") return new PresenceStanza();
            throw new NotImplementedException();
        }

        protected override void SetEmbeddedNodeValue(string nodeName, string value)
        {
            switch (nodeName)
            {
                case "show":
                    SetAvaibilityStatus(value);
                    break;

                case "status":
                    RootElement.StatusText = value;
                    break;

                case "priority":
                    SetPriorityValue(value);
                    break;
            }
        }

        private void SetAvaibilityStatus(string value)
        {
            switch (value)
            {
                case "":
                    RootElement.Availability = AvailabilityType.None;
                    break;

                case "away":
                    RootElement.Availability = AvailabilityType.Away;
                    break;

                case "chat":
                    RootElement.Availability = AvailabilityType.Chat;
                    break;

                case "dnd":
                    RootElement.Availability = AvailabilityType.DoNotDisturb;
                    break;

                case "xa":
                    RootElement.Availability = AvailabilityType.ExtendedAway;
                    break;
            }
        }

        private void SetPriorityValue(string value)
        {
            if (string.IsNullOrEmpty(value)) return;
            RootElement.Priority = int.Parse(value);
        }
    }
}
